<template>
	<view>
		<view class="list-call">
			<image class="img" src="/static/shilu-login/4.png"></image>
			<input class="biaoti" v-model="name" maxlength="11" placeholder="姓名" />
		</view>
		<view class="list-call">
			<image class="img" src="/static/shilu-login/1.png"></image>
			<input class="biaoti" v-model="phone" type="number" maxlength="11" placeholder="手机号" />
			<!-- <button class="cu-btn bg-green" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取本机号码</button> -->
		</view>
		<view class="list-call">
			<image class="img" src="/static/shilu-login/1.png"></image>
			<input class="biaoti" v-model="agentPhone" type="number" maxlength="11" placeholder="老板手机号" />
			<!-- <button class="cu-btn bg-green" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取本机号码</button> -->
			<text class="cuIcon-scan text-yellow" style="font-size: 42rpx;" @click="scanCodeBtnClick"></text>
		</view>
		<view class="btnContainer"><button class="cu-btn round bg-yellow" @click="regClick">注册</button></view>
	</view>
</template>

<script>
import { mapActions } from 'vuex';
import subMessageIds from '@/config/subMessageIds.js';
export default {
	props: {
		agentopenid: '',
		noReg: false
	},
	data() {
		return {
			name: '',
			phone: '',
			agentPhone: ''
		};
	},
	mounted() {
		if (this.$store.state.user.userInfo.openId) {
			this.initData();
		} else {
			uni.$on('getedOpenId', () => {
				this.initData();
			});
		}
	},
	methods: {
		...mapActions({
			agentStaffRegister: 'agent/agentStaffRegister',
			getServerUserInfo: 'user/getServerUserInfo',
			staffBindAgent: 'agent/staffBindAgent'
		}),
		initData() {
			if (!this.noReg && this.agentopenid) {
				Promise.all([this.getStaffInfo(), this.getAgentInfo(this.agentopenid)])
					.then(res => {
						if (res[0].No == res[1].No) {
							return {
								msg: '请勿绑定自己'
							};
						} else {
							return this.staffBindAgent({
								no: res[0].No,
								parentNo: res[1].No
							});
						}
					})
					.then(res1 => {
						console.log(res1);
						if (res1 && res1.code == '000000') {
							uni.showModal({
								content: res1.msg,
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
										uni.redirectTo({
											url: '/pages/index/index'
										});
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						} else {
							uni.showModal({
								content: res1.msg,
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						}
					})
					.catch(err => {
						console.log(err);
					});
			} else {
				if (!this.noReg) {
					this.getStaffInfo().catch(err => {
						console.log(err);
					});
				}
				if (this.agentopenid) {
					this.getAgentInfo(this.agentopenid);
				}
			}
		},
		getStaffInfo() {
			return new Promise((resolve, reject) => {
				this.getServerUserInfo({
					wxAppOpenId: this.$store.state.user.userInfo.openId
				})
					.then(res => {
						console.log(res);
						if (res && res.code == '000000') {
							this.phone = res.data.Tel;
							this.name = res.data.Name;
							resolve(res.data);
						} else {
							reject();
						}
					})
					.catch(err => {
						console.log(err);
						reject();
					});
			});
		},
		getAgentInfo(openId) {
			return new Promise((resolve, reject) => {
				this.getServerUserInfo({
					name: openId
				})
					.then(res => {
						console.log(res);
						if (res && res.code == '000000') {
							this.agentPhone = res.data.Tel;
							resolve(res.data);
						} else {
							reject();
						}
					})
					.catch(err => {
						console.log(err);
						reject(err);
					});
			});
		},
		regClick() {
			let self = this;
			wx.requestSubscribeMessage({
				tmplIds: [subMessageIds.receiveRegMsg],
				success(res) {
					console.log(res);
				},
				fail(err) {
					console.log(err);
				},
				complete() {
					if (!/^1[3456789]\d{9}$/.test(self.phone)) {
						uni.showToast({
							icon: 'none',
							title: '请输入正确的手机号'
						});
						return;
					}
					if (self.name == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入姓名'
						});
						return;
					}
					if (!/^1[3456789]\d{9}$/.test(self.agentPhone)) {
						uni.showToast({
							icon: 'none',
							title: '请输入老板手机号'
						});
						return;
					}
					uni.showLoading({
						title: '请求中',
						mask: true
					});
					self.agentStaffRegister({
						tel: self.phone,
						name: self.name,
						wxAppOpenId: self.$store.state.user.userInfo.openId,
						parentTel: self.agentPhone
					})
						.then(res => {
							uni.hideLoading();
							console.log(res);
							if (res) {
								switch (res.code) {
									case '000000': {
										uni.showModal({
											content: res.msg,
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.redirectTo({
														url: '/pages/index/index'
													});
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
										break;
									}
									default: {
										self.$nextTick(function() {
											uni.showModal({
												content: res.msg,
												showCancel: false,
												success: function(res) {
													if (res.confirm) {
														console.log('用户点击确定');
													} else if (res.cancel) {
														console.log('用户点击取消');
													}
												}
											});
										});

										break;
									}
								}
							}
						})
						.catch(err => {
							console.log(err);
							uni.hideLoading();
						});
				}
			});
		},
		scanCodeBtnClick() {
			let self = this;
			uni.scanCode({
				success(res) {
					console.log(res);
					if (res.path) {
						let params = decodeURIComponent(res.path).split('&');
						let obj = {};
						params.forEach((e, i) => {
							let arr = e.split('=');
							obj[arr[0]] = arr[1];
						});
						if (obj.id) {
							self.getAgentInfo(obj.id);
						}
					} else {
						uni.showToast({
							title: '请扫描正确的代理商小程序码',
							icon: 'none'
						});
					}
				}
			});
		}
	}
};
</script>

<style></style>
